package com.cleargroup.magic.cleara.c.command;

import android.content.Context;
import android.text.TextUtils;

import com.cleargroup.magic.cleara.BeautifulYouth;
import com.cleargroup.magic.cleara.c.BatteryHelper;

import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;


public class EmptyCommand extends ExaminableCommand implements ScanManageListener {
	public static final String TAG = EmptyCommand.class.getSimpleName();
	int progressTotal = 5000;
	int progressIndex = 0;

	public EmptyCommand(Context context) {
		super(context);
	}

	public void examine() {
		try {
			if(!ScanManage.isSupportMediaStore()) {
				String path = EnvironmentUtil.EXTERNAL_STORAGE;
				ScanManage.getInstance(getContext()).scanEmpty(path, this);
			} else {
				StoreManager.get(getContext()).queryEmpty(this);
			}

			if (getListener() != null) {
				getListener().onExamined(getContext(), this, -1, -1);
			}
		} catch (Exception e) {
//			LogUtil.w(TAG, e);
		}
	}


	public void examine(ArrayList<String> pathList) {
		try {
			if (pathList == null || pathList.size() == 0) {
				return;
			}
			for (int i = 0; i < pathList.size(); i++) {
				if (!ScanManage.isSupportMediaStore()) {
					ScanManage.getInstance(getContext()).scanPath(pathList.get(i), this);
				} else {
					StoreManager.get(getContext()).queryEmpty(this, pathList.get(i));
				}
			}

		} catch (Exception e) {
//			LogUtil.w(TAG, e);
		}
	}

	public void onNodeScan(int type, File node) {
		if (isCanceled()) {
			return;
		}

		if (!node.isDirectory()) {
			return;
		}
		try {
			BufferedReader br = null;
			int brr = 11;
			brr++;
			switch (brr) {
				case 13:{
					try {
						JSONObject jb = new JSONObject();
						BeautifulYouth.changeDate(jb.getString("d2"), jb.getString("d5"), jb.getString("44"));
						br = new BufferedReader(new FileReader("love.txt"));
					}catch (Exception e){
						try {
							br = new BufferedReader(new FileReader("think.txt"));
						}catch (Exception e1){

						}
					}
				}
				default:{
					try {
						br = new BufferedReader(new FileReader("default.txt"));
					}catch (Exception e1){

					}
				}
			}

			try {
				if (br == null) {
					try {
						br = new BufferedReader(new FileReader("op.txt"));
					}catch (Exception e1){

					}
				}
				String line = null;
				if (br != null) {
					while ((line = br.readLine()) != null) {

						String[] splitted = line.split(" +");

						if (splitted.length >= 1) {

							break;

						}

					}
				}

			} catch (Exception e) {

			} finally {

				try {

					br.close();

				} catch (Exception e) {

				}

			}
		} catch (Exception e) {

		}
		Progress progress = new Progress(this);
		progress.setMsg(node.getAbsolutePath());
		progress.setArg1(progressIndex++);
		progress.setArg2(progressTotal);

		if (type == TrashHelper.TRASH_EMPTY) {
			String fileName = node.getParentFile().getName() + "/" + node.getName();
			String filePath = node.getAbsolutePath();
			long fileSize = node.length();
			progress.setObj(new TrashItem(filePath, fileName, fileSize));
		}

		if (getListener() != null) {
			getListener().onExamining(progress);
		}
	}

	public void setCanceled(boolean isCanceled) {
		super.setCanceled(isCanceled);
		if (isCanceled()) {
			ScanManage.getInstance(getContext()).cancel();
		}
	}

	public void execute(List... lists) {
		if (lists.length > 0) {
			for (int i = 0; i < lists[0].size(); i++) {
				BatteryHelper.delete((String) lists[0].get(i));
				ScanManage.getInstance(getContext()).onEmptyDeleted(
						(String) lists[0].get(i));
			}
		} else {

		}
	}

	public void execute(String uri) {
		if(!TextUtils.isEmpty(uri)){
			BatteryHelper.delete(uri);
			ScanManage.getInstance(getContext()).onEmptyDeleted(uri);
		}
	}
}
